

%MACRO table1(in);

*****************************************************************************
* Program: table1			   						
* 																			
* Description: Produces Table1
*						
* Last modified: Feb 8, 2016												
* Created by: Martin Wegman													
*****************************************************************************;

%let date = %sysfunc(date(),DATE7.);
%let name = CDDC Urine Table 1 &date..rtf;
%let file_name = &out_dir\&name;

******************************************************
*************          Summary          **************
******************************************************
******************************************************;

*Create any summary variables if needed;
*Create/edit labels and formats if needed;

data comm;
	length	level 			$	150 
  			pvalue			$	17
  			ci c1 c2 		$	40;
  			
	nvar=23; _line=0.5; level="Readiness for change";		pvalue=''; 	output;
	nvar=29; _line=0.5; level="Social Support";				pvalue=''; 	output;
run;


proc format library = loc;
	value site_ 			1='CDDC' 
							2='Voluntary Treatment';
	value dm2_m_ 			1='Malay' 
							3='Indian' 
							2='Chinese and Other';
	value dm3_m_			0='No' 
							1='Yes';
	value dm4_m_ 			0='No' 
							1='Yes';
	value dm5_m_ 			1='Permanent' 
							2='Temporary';
	value du15_choice_m_ 	2='Heroin' 
							1='Other';
	value ny_ 				0='No' 
							1='Yes'
							.='Missing';
	value dast_cat_m_		0 = 'None Reported'
							2 = 'Low or Moderate'
							3 = 'Substantial'
							4 = 'Severe'
							.='Missing';
	value od_cat_ 			0='No' 
							1='Yes' 
							.='Missing';
	value cs01_craving_ 	00='00 (No craving whatsoever)' 
							01='01' 
							02='02' 
							03='03' 
							04='04' 
							05='05' 
							06='06' 
							07='07' 
							08='08' 
							09='09' 
							10='10 (I think about it all the time)' 
							98='Refuse to answer'
							.='Missing'; 
	value he03a_ 			0='HIV-negative' 
							1='HIV-positive' 
							2,3 ='Unknown'
							.='Missing';

run;

data  loc.&in.table;
	set  loc.&in;

	format site site_.;
	format dm2_m dm2_m_.;
	format dm3_m dm3_m_.;
	format dm4_m dm4_m_.;
	format dm5_m dm5_m_.;
	format DU15_choice_m DU15_choice_m_.;
	format rbd1 ny_.;
	format dast_cat_m dast_cat_m_.;
	format od_cat od_cat_.;
	format cs01_craving cs01_craving_.;
	format dt01 ny_.;
	format dt01b ny_.;
	format he01 ny_.;
	format he02 ny_.;
	format he03a he03a_.;
	format 	daily_heroin 
			lifetime_alcohol 
			lifetime_moreuse
			lifetime_other_opiates 
			lifetime_benzos 
			lifetime_stimulants ny_.;

	label dm0 = "Age (years)";
	label dm2_m = "Ethnicity, n(%)";
	label dm3_m = "Completed secondary school, n(%)";
	label dm4_m = "Married, n(%)";
	label dm5_m = "Previous housing type, n(%)";
	label IH01_Prison="Times imprisoned";
	label IH02_Lockup="Times in lockup/jail";
	label IH05_totaltimes="Times detained in CDDC*";
	label DU15_choice_m="Drug of choice, n(%)";
	label DU16_agefirstdrug="Age of first drug use (years)";
	label RBD1="Ever injected drugs, n(%)";
	label dast_cat_m="Drug use severity, n(%)";
	label od_cat="Opiate dependence, n(%)";
	label CS01_craving="Opiate cravings (0-10)";
	label so_rec_d="Recognition";
	label so_amb_d="Ambivalence";
	label so_ts_d="Taking Steps";
	label DT01="Ever received buprenorphine treatment*, n(%)";
	label DT01B="Recent buprenorphine treatment*, n(%)";
	label HE01="Recent emergent/urgent care, n(%)";
	label HE02="Ever tested for HIV, n(%)";
	label HE03A="HIV test result, n(%)";	
	label ss_so="Significant other";
	label ss_fam="Family";
	label ss_fri="Friends";
	label du02b_heroin_years="Years of heroin use";
	label daily_heroin="Daily use of heroin before entering facility, n(%)";
	label lifetime_alcohol="Alcohol use (lifetime), n(%)";
	label lifetime_moreuse="Use of >1 drug at same time (lifetime), n(%)";
	label lifetime_other_opiates="Non-heroin opiate use (lifetime), n(%)";
	label lifetime_benzos="Benzodiazepine use (lifetime), n(%)";
	label lifetime_stimulants="Stimulant use (lifetime), n(%)";
	label lifetime_stimulants="Stimulant use (lifetime), n(%)";
	label T_dis="Time inpatient (days)";

	if site = 1 then do;
		ih05_totaltimes = ih05_totaltimes  - 1; 
		* Must reflect characteristic prior to group assignment;
	end;
	if site = 1 and IH02_Lockup > 0 then do;
		IH02_Lockup = IH02_Lockup - 1;
		* Same as above;
	end;

run; 

%let master = loc.&in.table;
%let var_list = dm0 dm2_m dm3_m dm4_m dm5_m
				ih01_prison ih02_lockup ih05_totaltimes
				du16_agefirstdrug du15_choice_m 
				du02b_heroin_years
				daily_heroin
				dast_cat_m cs01_craving
				rbd1
				lifetime_alcohol lifetime_other_opiates 
				lifetime_benzos lifetime_stimulants 
				lifetime_moreuse 
				dt01 dt01b
				so_rec_d so_amb_d so_ts_d
				he01 he02 he03a  
				ss_so ss_fam ss_fri T_dis ;	
				
%let var_type = c d d d d
				c c c
				c d
				c
				d
				d c
				d
				d d 
				d d
				d
				d d
				c c c
				d d d
				c c c c;

%let var_dvar = 5 10 12 13 15 20 21 22 26 27 28;
%let var_dline = 2 2 2 2 2 2 2 2 2 2 2;
%let FOOT = Missing data < 5% for a given variable is not indicated;
%let FOOT2 = *12% nonresponse in voluntary treatment arm;

%table(	dsn = &master, /* input dataset */
 		var = &var_list, /* analysis variables */
 		type = &var_type, /* analysis variable types */
		total=n,
		pfoot=y,
		date = n,
		TITLEFNT= Times,
		headfnt = Times,
		bodyfnt = Times,
		MEDDEC = 0,
		RNGDEC  = 0,
		QDEC = 0,
		cstats   = median quartiles range,
		LABELWRAP = y,
		dvar = &var_dvar,
		dline = &var_dline,
		FOOT = &foot,
		FOOT2 = &foot2,
		TITLESZ = 10,
		BODYSZ  = 8,
		FOOTSZ  = 10,
		comments = comm,
		by = site, /* group variable */
 		outdoc = &file_name
); /* output RTF file name */

/*
proc means data = loc.&dat.&version.table 
N NMISS MEAN STD MIN Q1 MEDIAN Q3 MAX fw=5;
	var 		dm0 
				ih01_prison 
				ih02_lockup 
				ih05_totaltimes
				du16_agefirstdrug
				du02b_heroin_years
				cs01_craving
				so_rec_d so_amb_d so_ts_d  
				ss_so ss_fam ss_fri;
run;

proc sort data = loc.&dat.&version.table; by site; run;

proc freq data = loc.&dat.&version.table;
by site;
table ih01_prison Ih02_lockup ih05_totaltimes;
format ih01_prison Ih02_lockup  ih05_totaltimes miss_.;
run;*/

/*
Missing >5%
1% and 5% missing for Ever tested for HIV
3% and 6% missing for HIV test result
3% and 7% missing for Daily use of heroin before entering facility
0% and 12% missing for Ever received suboxone treatment
2% and 12% missing for Times detained in CDDC
*/


****************************************************************************;
****************************************************************************;
*********************   Remove old datasets               ******************;
****************************************************************************;
****************************************************************************;

proc datasets lib=loc nolist;       
	delete &in.table;  
run;
quit;

****************************************************************************;
****************************************************************************;
**************                END                ***************************;
****************************************************************************;
****************************************************************************;
%MEND table1;




